﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using System.IO;

namespace LaserMark.Components.OPTFileResovle
{
    public static class OPFileConfigurationExtensions
    {
        public static IConfigurationBuilder AddOPTFile(this IConfigurationBuilder builder, string path)
        {
            return AddOPTFile(builder, provider: null, path: path, optional: false, reloadOnChange: false);
        }

        public static IConfigurationBuilder AddOPTFile(this IConfigurationBuilder builder, string path, bool optional)
        {
            return AddOPTFile(builder, provider: null, path: path, optional: optional, reloadOnChange: false);
        }

        public static IConfigurationBuilder AddOPTFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange)
        {
            return AddOPTFile(builder, provider: null, path: path, optional: optional, reloadOnChange: reloadOnChange);
        }

        public static IConfigurationBuilder AddOPTFile(this IConfigurationBuilder builder, IFileProvider? provider, string path, bool optional, bool reloadOnChange)
        {
            return builder.AddOPTFile(s =>
            {
                s.FileProvider = provider;
                s.Path = path;
                s.Optional = optional;
                s.ReloadOnChange = reloadOnChange;
                s.ResolveFileProvider();
            });
        }

        public static IConfigurationBuilder AddOPTFile(this IConfigurationBuilder builder, Action<OPTFileConfigurationSource>? configureSource)
            => builder.Add(configureSource);

        public static IConfigurationBuilder AddOPTFileStream(this IConfigurationBuilder builder, Stream stream)
        {
            return builder.Add<OPTFileResovle.OPTStreamConfigurationSource>(s => s.Stream = stream);
        }
    }
}
